﻿using AutoMapper;
using System;
using Local = Sustainalytics.ESG.Entities;
using Shared = Sustainalytics.SharedEntities.EsgResearch;

namespace Sustainalytics.ClientDW.Synchronization.SyncESGData
{
    public static class AutoMapperConfig
    {
        public static void RegisterMappings()
        {
            Mapper.Reset();
            Mapper.Initialize(Config);
        }

        private static void Config(IMapperConfigurationExpression cfg)
        {
            cfg.CreateMissingTypeMaps = true;
            cfg.AllowNullCollections = true;
            cfg.AllowNullDestinationValues = true;

            cfg.CreateMap(typeof(Shared.Timestamped<>), typeof(Timestamped<>));
            cfg.CreateMap<Shared.RPCorporateScore, Local.RPRelativePosition>();
            cfg.CreateMap<Shared.RPRelativePosition, Local.RPRelativePosition>();
            cfg.CreateMap<Shared.RPAssessment, Local.RPAssessment>();
            cfg.CreateMap<Shared.RPPerformance, Local.RPPerformance>();
            cfg.CreateMap<Shared.AnswerCategory, Entities.AnswerCategory>();            
            cfg.CreateMap<Shared.RPIndicatorPerformance, Local.RPIndicatorPerformance>();
            cfg.CreateMap<Shared.RPCompanyESGComputationResult, Local.RPCompanyESGComputationResult>();


        }
    }
}

namespace Sustainalytics
{
    public static class SharedTimestampedExtensions
    {
        public static Shared.Timestamped<U> Bind<T, U>(this Shared.Timestamped<T> timestamped, Func<T, U> func)
        {
            if (timestamped == null || Equals(timestamped.Value, null))
                return null;

            return new Shared.Timestamped<U> { Value = func(timestamped.Value), Timestamp = timestamped.Timestamp };
        }
    }
}
